﻿@using System.Web.Mvc.Html
@model ECatalog.Models.Models.ModelView
@{
    Layout = "../Shared/_Layout.cshtml";
}
<div class="container">
    @using (@Html.BeginForm("SaveModel", "Model", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {        
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m => m.ModelId)
        @Html.HiddenFor(m => m.TypeImage)
        @Html.HiddenFor(m => m.ImageUrl)
        <div class="row">
            <div class="photo-editor">
                @{
        var imageUrl = "";
        if (Model != null)
        {
            imageUrl = string.Format("/Files/Organization-{0}/Model-{1}/{2}/800x600/{3}{4}", @Model.OrganizationId, @Model.ModelId, @Model.ImageUrl, @Model.ImageUrl, @Model.TypeImage);
        }
                }
                <img class="photo-block" src="@imageUrl" alt="" />
                <input type="file" name="UploadedFile" id="UploadFile" class="form-control upload-image-button btn btn-default" value="@imageUrl" />
            </div>
            <div class="description-editor">
                <div style="width: 300px;">
                    <p>Организация</p>
                    @if (Model != null)
                    {
                        @Html.DropDownListFor(m => m.OrganizationId, new SelectList(Model.Organizations, "OrganizationId", "Name"), new { @class = "form-control" })
                    }

                    @Html.ValidationMessageFor(model => model.OrganizationId)
                </div>
            </div>
            <div class="description-editor" style="height: 270px;">
                <span class="description-text">
                    <p>Описание</p>
                </span>
                @Html.TextAreaFor(model => model.Description, new { @class = "description-block" })
                @Html.ValidationMessageFor(m => m.Description)
            </div>
        </div>
        <div class="row">
            <div style="margin-left: 340px; width: 800px;">
                <div class="form-group col-lg-3">
                    <label class="control-label">Номер каталога</label>
                    @Html.TextBoxFor(model => model.CatNumber, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.CatNumber)
                </div>
                <div class="form-group col-lg-3">
                    <label class="control-label">Цена</label>
                    @Html.TextBoxFor(model => model.Price, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Price)
                </div>
                <div class="form-group col-lg-3">
                    <label class="control-label">Скидка(%)</label>
                    @Html.TextBoxFor(model => model.Sale, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Sale)
                </div>
            </div>
        </div>
        <div style="width: 270px;">
            <p>
                @if (Model != null)
                {
                    if (Model.ShopModelSizes != null)
                    {
                        foreach (var shop in Model.Shops)
                        {
                    @Html.ActionLink(string.Format("Расположение в {0}", shop.Name), "EditLocation", new { modelId = Model.ModelId, shopId = shop.ShopId }, new { @class = "btn btn-default add-location-button" })
                        }
                    }
                    else
                    {
                    @Html.ActionLink("Редактировать расположение", "EditLocation", new { modelId = Model.ModelId }, new { @class = "btn btn-default add-location-button disabled", title = "Нет размеров" })
                    }
                }
            </p>
        </div>
        <p>
            <input type="submit" class="btn btn-primary" value="Сохранить модель" />
            @Html.ActionLink("К списку моделей", "Index", "Model", null, new { @class = "btn btn-link" })
        </p>
    }

    @if (Model != null && Model.ModelId != 0)
    {
        <table>
            <tr>
                <td class="top">
                    <div style="width: 300px;margin-top:20px; display: block; float: left;">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h3 class="panel-title">Цвета</h3>
                            </div>
                            <div class="panel-body panel-color">
                                @if (Model != null && Model.Colors.Any())
                                {
                                    foreach (var item in Model.Colors)
                                    {
                                    <div class="color-item-data">
                                        <div class="preview-read" style="background-color: rgb(@item.RgbNumber);float: left;"></div>
                                        <span>&nbsp;&nbsp;&nbsp;@item.Name</span>
                                    </div>
                                    }
                                }
                                <div style="display: block;">
                                    <a data-toggle="modal" href="#colorModal" class="btn btn-default">Редактировать</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
                <td class="top">
                    <div class="panel-structures">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h3 class="panel-title">Структура</h3>
                            </div>
                            <div class="panel-body">
                                @if (Model != null && Model.Structures.Any())
                                {
                                    foreach (var item in Model.Structures)
                                    {
                                    <div class="structure-item-data">
                                        <span>@item.Name</span>
                                        <span>@item.Value</span>
                                    </div>
                                    }
                                }
                                <div style="display: block;"><a data-toggle="modal" href="#structureModal" class="btn btn-default">Редактировать</a></div>
                            </div>
                        </div>
                    </div>
                    @if (Model != null && Model.ShopModelSizes.Any())
                    {
                        foreach (var item in Model.ShopModelSizes)
                        {
                        <div class="panel-sizes">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <h3 class="panel-title">Размеры в @item.Shop.Name</h3>
                                </div>
                                <div class="panel-body">
                                    @foreach (var size in item.ModelSizeDtos)
                                    {
                                        <div class="size-item-data">
                                            <span>@size.Value</span>
                                        </div>
                                    }
                                    <div style="display: block;"><a data-toggle="modal" href="#sizeModal-@item.Shop.ShopId" class="btn btn-default">Редактировать</a></div>
                                </div>
                            </div>
                        </div>
                        }
                    }
                    else
                    {
                        <div style="display: block;">
                            Нет размеров.
                        </div>
                    }
                </td>
            </tr>
        </table>
                    Html.RenderPartial("Size", Model);
                    Html.RenderPartial("ColorModal", Model);
                    Html.RenderPartial("StructureModal", Model);
    }
</div>

@section scripts
{
    <script type="text/javascript">
        $(document).on("click", ".remove-structure", function () {
            var div = $(this);
            var id = div.attr("id");
            $.ajax({
                type: "POST",
                url: "/Structure/Remove",
                data: "structureid=" + id,
                success: function (msg) {
                    div.closest('div').parent().hide();
                }
            });
        });

        $(document).on("click", ".remove-color", function () {
            var div = $(this);
            var id = div.attr("id");
            $.ajax({
                type: "POST",
                url: "/Color/Remove",
                data: "colorid=" + id,
                success: function (msg) {
                    div.closest('div').parent().hide();
                }
            });
        });

        $(document).on("click", ".remove-modelsize", function () {
            var div = $(this);
            var id = div.attr("id");
            $.ajax({
                type: "POST",
                url: "/SizeModel/Remove",
                data: "modelsizeid=" + id,
                success: function (msg) {
                    div.closest('div').parent().hide();
                }
            });
        });

        $(document).ready(function () {
            $("#RgbNumber").val("255,255,255");
        });
    </script>
    <script src="~/Scripts/color-picker.js"></script>
}

